﻿using B.S.DataBase.MyProject.Domain.procedure;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ApprovalCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ApprovalCommandHandler
{
    /// <summary>
    /// 审批流程修改
    /// </summary>
    public class UpdaterovalTrailCommandHandler : IRequestHandler<UpdaterovalTrailCommand, APIResult<object>>
    {
        /// <summary>
        /// 审批管理基本信息
        /// </summary>
        private readonly IBaseRepository<ApprovalTrail> baseRepository;

        /// <summary>
        /// 流程
        /// </summary>
        private readonly IBaseRepository<AppProcess> appPritory;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        public UpdaterovalTrailCommandHandler(IBaseRepository<ApprovalTrail> baseRepository, IBaseRepository<AppProcess> appPritory)
        {
            this.baseRepository = baseRepository;
            this.appPritory = appPritory;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<object>> Handle(UpdaterovalTrailCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result =new APIResult<object>();
            result.Code = APIEnum.修改成功;
            result.Msg = APIEnum.修改成功.ToString();
            var list = baseRepository.GetAll().Result.FirstOrDefault(x=>x.Addroveid==request.Addroveid);
            var stttig =appPritory.GetAll().Result.ToList();

            if (list.Belongto.Length > 1)
            {
                list.Addroveid = request.Addroveid;
                list.Userid = (int)stttig[2].Userid;
                list.UserNiken = stttig[2].UserNiken;
                list.Status = (int)States.已拒绝;
                list.Appstatus = States.已拒绝.ToString();
                list.RoleName = stttig[2].RoleName;
                list.Belongto = request.Belongto;//意见

            }
            else
            {
                list.Addroveid = request.Addroveid;
                list.Userid = (int)stttig[0].Userid;
                list.UserNiken = stttig[0].UserNiken;
                list.Status = (int)States.审批中;
                list.Appstatus = States.审批中.ToString();
                list.RoleName = stttig[0].RoleName;
                list.Belongto = request.Belongto;//意见
            }
            if(list.Userid == 1)
            {
                list.Addroveid = request.Addroveid;
                list.Userid = (int)stttig[0].Userid;
                list.UserNiken = stttig[0].UserNiken;
                list.Status = (int)States.已通过;
                list.Appstatus = States.已通过.ToString();
                list.RoleName = stttig[0].RoleName;
                list.Belongto = request.Belongto;//意见
            }


            baseRepository.Update(list);
            return Task.FromResult(result);
        }
    }
}
